package com.lzc.tlmail.tlmailorder.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.lzc.tlmail.tlmailorder.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {



    //在支付宝创建的应用的id
    private   String app_id = "2021000116681926";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCYzLW09/3QvP1fO+HPDvdzfDL2AdX2+0T5kPPEVNyEyu1nDamoTa7x1v/yq1A2U+iqlFpMxuVKYTx1DWnnAex5ugH3d+OaUfdpV8I7MIvInUKklebqnRmy0//flNlSmK9NN9IPI+gpETG71jgOjsn9Al8JA0ktpgHShUX1Ai85mUuVIjSfDP2Per1pylD2H3zidlIycpMA4huz0ebVRqPA1fOGz7y5Hj0weOlN71diYKoCUY6pyTdZ1fB1SrmwjauKjatsBNCKyTlZ5fJZ48AuRdQRBkjm2xG7OkrFNPVeGVXVXiTPWVlniGoXXDb+XvcUtPHOFmVlA+IAyWhupbSPAgMBAAECggEBAJi0PQRWsrphpXX/inQ8KOCpRuTEgOkLxKsFShj0Fk4/33IWHW+t8yCOFcyXmNoykVuoV3ZTb+MduiXg+e175rDgTAbdMgGP2QrWQa6VnZ0oTOnfRZ9C0eP+KsFvKKF7JfatyqiG4gXowSNPu0YRPZw1OnTva7rLJRcXSgrJ3BhaoEdVY/qUrdbsTz4hTxl/Lpc/jxhadHtQkuDyAkcX2JHpYHLVgHNkS+oqH/rpQtY9yZp8a/10zla6zQm+V9iF53pnosugKfw2q/a5E72auai+PwCMjnuI0okg1uJvQkbMDmuV1tSLErgV15k/bCxKL+uT6wnh2/nYZMunFVO1CoECgYEA8Mvg4ZG+fTm81ehhW0FJqik+1IoGI9sL8qf79phFatQU6MYYs4iVUd1PuCOddk61DAETms4F3MUBzPCeffi3nsPQeso0lATIIIJvM0uSI4msSxFvSuyfSNlRDviJCZm+z4HhZoOhex6pvpninyoSnH4tJVcsBVFTGNq23ttEAekCgYEAonJ+GTxNL7WKuVfB5l+iOpMDXyu/Z0bWQt5Z8aQfNfKH/3q+JwRNQrJaBqakECuGuGUXh2DvXanGRZVzeUKV+sOdln53wRZW/hXjdZQoDuL7YIp5IkfQmhClKSZ7z4njDnEdHPAfpz0cRW5wOPHCmNp+lGhbM9kTQeTkeWYQP7cCgYB2wQWyGU+psIzprcY93A/5ZIgBB+UJT+EvlhsUPXqWuetp9TB/ck+55PyI4r+odh6hwfIqOYr4mwxNRynTFJNyMD4kII8XpNgR+GHhGVcEQcR/7SRG/jV8Fvj7+iSTYIiTDAnepfJApxmtz3TNbFFrbmrTbI9gPABQVKU1eUur6QKBgD7M9GLdJRnknRS6EIP3RMhQPWV1jDFo+TwwqaT5RfedI2ZpFUoQJ645NQSNN6/oo9R1gHsdxmv3TmdoN5QRYdnn9exQSAulj0v7OkwiUroule/kmJ/gE4z8El2uOv8o11al9BNT4WjN/zf4UfoxvIFLNtQ448lPilpXGtyz0GRRAoGAYneBOP0F6LZKTyhe3ddThsrJHRjq7BOhkS35InK9FW0Vz5lnNJzGERHfgUA4ndbfItVARjHTqjbHCyQuS813s3bEUxJd61S35GWU9fW1Vko5PuAIqgcQaJiZw8n4Gxs0Am3Gpz3AGS2c8hQLQoFs/qWosSinv2fFw8b3VrgZU/s=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApo86CoC2gtY7kpM/Z+LpFLzkG06s2hGyimAt/GQ75zBci5snhiHp7r4jUztQmeXUaigT65V4NP2WXotPCq/Bylnpa9wPQF2f7k+8ij9xnWuFqYn1/ig6NeLttYdF0KVOpu1s3NvY4z1ulMKy8S0UndSxVIq4yd8y0WEwDLWPE9EuzO3MPjdyKr4cd9ww2w0lqVNY3w+cbC9eASxl+X04fjyoFz51WPogL4gmP4rx2Huiqr38jnGOuUzEBNnkT6WsCO6u+LPgcrASJwA5mfLHZIsE5Vgcvqb8MaKallV2p+wKihPxQnzQ6Jhf4MXroLg1EEN5ZTHkaO+hzyJkOmFV3wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://www.order.tlmail.com/orderList";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://www.order.tlmail.com/orderList";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";
    //支付超时时间
    private String timeout="30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    //https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
